package algorithm.异或;

/**
 * @ Description：数组中只有个两个数出现了寄数次，打印出结果
 */
public class Demo {
    public static void main(String[] args) {

//        int a = 5;
//        System.out.println("a = " + a);
//        System.out.println("-a = " + (~5 + 1));

        int[] arr = new int[]{1, 1, 1, 2, 2, 3, 3, 4, 4};
        print1(arr);
        int[] arr1 = new int[]{1, 1, 1, 2, 2, 3, 3, 4, 4, 4};
        print2(arr1);
    }

    private static void print1(int[] arr) {
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        System.out.println(eor);
    }

    private static void print2(int[] arr) {
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        int eor1 = 0;
        int r1 = eor & (-eor);
        for (int i = 0; i < arr.length; i++) {
            if ((r1 ^ arr[i]) == 0) {
                eor1 ^= arr[i];
            }
        }
        System.out.println(eor1);
        System.out.println(eor ^ eor1);
    }
}
